## This software is licensed by the MIT License, see LICENSE file ##
##                                    Copyright © 2022 whitemangl ##

from typing import Union
import asyncio
from aiogram.types import Message

from utils     import TGPBot
from keyboards import ConfirmationKeyboard

__LATENCY__ = 0.75



async def __on_prod_channel_message(message: Message, bot: TGPBot):
    await asyncio.sleep(0)

async def __on_tech_channel_message(message: Message, bot: TGPBot):
    if not message.media_group_id:
        await message.edit_reply_markup(reply_markup=ConfirmationKeyboard())
        return
    if (message.media_group_id not in bot.message_groups):
        msg = f"Публикуем <a href=\"https://t.me/c/{bot.tech_channel}/{message.message_id}\">запись</a>?"
        bot.message_groups.update([(message.media_group_id, list()), ])

        await asyncio.sleep(__LATENCY__)

        media = [message,]
        media.extend(bot.message_groups.pop(message.media_group_id))
        bot.message_groups.update([(message.message_id, media), ])

        await bot.send_message(message.chat.id, msg, reply_markup=ConfirmationKeyboard())

    elif (isinstance(bot.message_groups[message.media_group_id], list)):
        bot.message_groups[message.media_group_id].append(message)

async def __on_ignore(message: Message, bot: TGPBot):
    await asyncio.sleep(0)

async def on_channel_message(message: Message, bot: TGPBot):
    print(message)
    if   message.chat.id == int(bot.prod_channel): await __on_prod_channel_message(message, bot)
    elif message.chat.id == int(bot.tech_channel): await __on_tech_channel_message(message, bot)
    else: await __on_ignore(message, bot)
